﻿using System;
using System.Linq;

namespace OrderExer
{
    internal class Program
    {
        static void Main(string[] args)
        {
            OrderService service = new OrderService();
            OrderService1(service);
        }



        public static void OrderService1(OrderService service)
        {
            Console.WriteLine("查询订单1，增加订单2，删除订单3，修改订单4,排序订单5，序列化订单6，从XML文件中载入订单7，退出8");
            while (true)
            {
                int choice = int.Parse(Console.ReadLine());
                switch (choice)
                {
                    case 1:
                        Console.WriteLine("按照订单号选择1，按照商品名称选择2,按照客户选择3，按照订单金额选择4");
                        int choice1 = int.Parse(Console.ReadLine());
                        switch (choice1) 
                        {
                            case 1:
                                var byOrderNo = from x in service.orderList where x.OrderNo > 0 orderby x.Amount select x;
                                foreach (Order order in byOrderNo)
                                {
                                    Console.WriteLine(order.ToString());
                                }
                                Console.WriteLine("显示完毕");
                                break;
                            case 2:
                                Console.WriteLine("输入商品名称");
                                string cname2 = Console.ReadLine();
                                var byOrderCname = from x in service.orderList where x.Cname == cname2 orderby x.Amount select x;
                                foreach (Order order in byOrderCname)
                                {
                                    Console.WriteLine(order.ToString());
                                }
                                Console.WriteLine("显示完毕");
                                break;
                            case 3:
                                Console.WriteLine("输入客户名称");
                                string name2 = Console.ReadLine();
                                var byCustomerName = from x in service.orderList where x.Customername == name2 orderby x.Amount select x;
                                foreach (Order order in byCustomerName)
                                {
                                    Console.WriteLine(order.ToString());
                                }
                                Console.WriteLine("显示完毕");
                                break;
                            case 4:
                                Console.WriteLine("输入金额");
                                int money = int.Parse(Console.ReadLine());
                                var byAmount = from x in service.orderList where x.Amount == money orderby x.Amount select x;
                                foreach (Order order in byAmount)
                                {
                                    Console.WriteLine(order.ToString());
                                }
                                Console.WriteLine("显示完毕");
                                break;
                            default:
                                Console.WriteLine("输入有误！");
                                break;
                        }

                        break;
                    case 2:
                        Console.WriteLine("输入订单号");
                        int no = int.Parse(Console.ReadLine());
                        Console.WriteLine("输入客户名字");
                        string name = Console.ReadLine();
                        Console.WriteLine("输入商品名字");
                        string cname= Console.ReadLine();
                        Console.WriteLine("输入总金额");
                        int amount = int.Parse(Console.ReadLine());
                        Console.WriteLine("输入该订单的订单明细数量");
                        int number = int.Parse(Console.ReadLine());
                        Order order1 = new Order(no, name, cname, amount, number);
                        service.addOrder(order1);
                        break;
                    case 3:
                        Console.WriteLine("输入订单号");
                        int n = int.Parse(Console.ReadLine());
                        service.deleteOrder(n);
                        break;
                    case 4:
                        Console.WriteLine("输入订单号");
                        int n2 = int.Parse(Console.ReadLine());
                        Console.WriteLine("输入该订单修改后的商品名称");
                        string modiname = Console.ReadLine();
                        service.modifyOrder(n2, modiname);
                        break;
                    case 5:
                        Console.WriteLine("按照订单号排序1,按照金额数量排序2");
                        int choice5 = int.Parse(Console.ReadLine());
                        switch (choice5) 
                        {
                            case 1:

                                service.sort();

                                foreach (Order order in service.orderList)
                                {
                                    Console.WriteLine(order.ToString());
                                }
                                Console.WriteLine("显示完毕");
                                break;
                            case 2:

                                service.sort((x, y) => x.Amount.CompareTo(y.Amount));

                                foreach (Order order in service.orderList)
                                {
                                    Console.WriteLine(order.ToString());
                                }
                                Console.WriteLine("显示完毕");
                                break;
                            default:
                                Console.WriteLine("输入有误！");
                                break;
                        }
                     
                        break;
                    case 6:
                        service.Export();
                        break;
                    case 7:
                        service.Import();
                        break;
                    case 8:
                        Environment.Exit(0);
                        break;
                    default: Console.WriteLine("输入有误！");break;

                }
            }
           

        }
    }
    
}
